package com.mq.springboot.controller;

import com.mq.springboot.config.RabbitMqConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.listener.api.RabbitListenerErrorHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author zhanghaidong
 * @version v1.0
 * @description
 * @date 2019/8/16
 */
@Component
public class Consumer {

    private static final Logger LOGGER = LoggerFactory.getLogger(Consumer.class);

    @Bean
    public RabbitListenerErrorHandler rabbitListenerErrorHandler(){
        return (amqpMessage, message, exception) -> {
            System.out.println(message);
            throw exception;
        };
    }

    @RabbitListener(queues = RabbitMqConfig.QUEUE_NAME, errorHandler = "rabbitListenerErrorHandler")
    public void consumeMessage(String message) {
        LOGGER.info("consume message {}", message);
    }

    @RabbitListener(queues = RabbitMqConfig.QUEUE_NAME, errorHandler = "rabbitListenerErrorHandler")
    public void consumeMessage1(String message) {
        LOGGER.info("consume1 message {}", message);
    }
}
